﻿#if WINDOWS_PHONE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO.IsolatedStorage;
using System.IO;

namespace DARE
{
    public class CStorage : IStorage
    {
#region Fields

        private PlayerIndex m_index;
        private IsolatedStorageFile m_files;

        #endregion

#region Property

        public bool IsSynchronize { get; private set; }

        public long TotalSpace
        {
            get { return m_files.Quota; }
        }

        public long FreeSpace
        {
            get { return m_files.AvailableFreeSpace; }
        }

        #endregion

#region Event

        public event Action<IStorage> StorageReady;
        public event Action<IStorage> StorageFailed;

        #endregion

#region CTOR

        public CStorage()
        {
            m_index = PlayerIndex.One;
            IsSynchronize = false;
            Synchronize();
        }

        public CStorage(PlayerIndex _index)
        {
            m_index = _index;
            IsSynchronize = false;
            Synchronize();
        }

        #endregion

#region Method

        public void CreateDirectory(string directory)
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            m_files.CreateDirectory(directory);
        }

        public System.IO.Stream CreateFile(string file)
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            return m_files.CreateFile(file);
        }

        public void DeleteDirectory(string directory)
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            m_files.DeleteDirectory(directory);
        }

        public void DeleteFile(string file)
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            m_files.DeleteFile(file);
        }

        public bool DirectoryExists(string directory)
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            return m_files.DirectoryExists(directory);
        }

        public void Dispose()
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            m_files.Dispose();
            m_files = null;
            IsSynchronize = false;
        }

        public bool FileExists(string file)
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            return m_files.FileExists(file);
        }

        public string[] GetDirectoryNames()
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            return m_files.GetDirectoryNames();
        }

        public string[] GetDirectoryNames(string searchPattern)
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            return m_files.GetDirectoryNames(searchPattern);
        }

        public string[] GetFileNames()
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            return m_files.GetFileNames();
        }

        public string[] GetFileNames(string searchPattern)
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            return m_files.GetFileNames(searchPattern);
        }

        public System.IO.Stream OpenFile(string file, System.IO.FileMode fileMode)
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            return m_files.OpenFile(file, fileMode);
        }

        public System.IO.Stream OpenFile(string file, System.IO.FileMode fileMode, System.IO.FileAccess fileAccess)
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            return m_files.OpenFile(file, fileMode, fileAccess);
        }

        public System.IO.Stream OpenFile(string file, System.IO.FileMode fileMode, System.IO.FileAccess fileAccess, System.IO.FileShare fileShare)
        {
            //if (!IsSynchronize)
            //    throw new Exception(Resources.StorageNotSynchronide);
            return m_files.OpenFile(file, fileMode, fileAccess, fileShare);
        }

        public void Synchronize()
        {
            m_files = IsolatedStorageFile.GetUserStoreForApplication();
            if (StorageReady != null)
                StorageReady.Invoke(this);
            IsSynchronize = true;
        }

        #endregion
    }
}
#endif
